/**
 * 恭喜获得界面
 */
class CongratulationsUI extends UIBase{
    private img_icon:eui.Image;
    private lb_count:eui.Label;
    private btn_sure:BaseButton;
    private img_light:eui.Image;
    private awardList=[];
    private _init = false;
    private curAward:string = '';
    private img_light1:eui.Image;
    private img_di_2:eui.Image;
    private img_di_1:eui.Image;
    private btn_show:BaseButton;
    private lb_use:eui.Label;
    private btn_close:BaseButton;
    constructor(data)
    {
        super('CongratulationsView', data);
    }

    public init()
    {
        super.init(true);
        let award:string = this.data.awardStr;
        let awardArr = award.split(',');
        this.awardList = awardArr;

        this._init = true;
        this.initUI();
    }

    public initUI()
    {
        if(this.awardList.length > 0)
        {
            this.curAward = this.awardList.shift();
            let awardData = ResourceUtil.instance().parseSignleAward(this.curAward);
            this.lb_count.text = awardData.desc;
            this.img_icon.source = awardData.itemIcon;
            if(awardData.itemData.type == 3)
            {
                this.img_icon.width = this.img_icon.texture.textureWidth;
                this.img_icon.height = this.img_icon.texture.textureHeight;
                this.img_light.visible = false;
                this.lb_use.visible = true;
                this.img_light1.visible = true;
                this.img_di_1.visible = false;
                this.img_di_2.visible = true;
                this.btn_show.visible = true;
                this.btn_sure.visible = false;
                this.btn_close.visible = true;
            }
            else
            {
               this.img_icon.width = this.img_icon.height = 100; 
               this.img_light.visible = true;
               this.lb_use.visible = false;
                this.img_light1.visible = false;
                this.img_di_1.visible = true;
                this.img_di_2.visible = false;
                this.btn_close.visible = false;
                this.btn_sure.visible = true;
                this.btn_show.visible = false;
            }
        }
    }

    public addEvent()
    {
        this.btn_sure.addTapHandler(this.close, this);
        this.btn_show.addTapHandler(this.show, this);
        this.btn_close.addTapHandler(this.close, this);
        this.lb_use.addEventListener(egret.TouchEvent.TOUCH_TAP, this.useHandle, this);
    }

    public useHandle()
    {
        let awardData = ResourceUtil.instance().parseSignleAward(this.curAward);
        let type = awardData.itemData.type;
        if(type != 3)return;
        let weaponId = parseInt(awardData.itemData.relatedId);
        let da = {
            weapon_id:weaponId
        }
        WeaponPrase.instance().useWeapon(da, this.close, this);
    }

    public show()
    {
        let awardData = ResourceUtil.instance().parseSignleAward(this.curAward);
        let type = awardData.itemData.type;
        if(type != 3)return;
        let name = awardData.itemData.name;
        let title = `我有神兵利器${name}, 尔等还不退下！`;
        SharePrase.instance().requestOneAdvertisement({type:ShareId.show_weapon,title:title});
    }

    public close()
    {
        ResourceUtil.instance().addResource(this.curAward);
        if(this.awardList.length > 0)
        {
            this.initUI();
        }
        else
        {
            if(this.data.call)
            {
                let call:Function = this.data.call;
                let scope = this.data.scope;
                call.call(scope);
            }
            this.remove(4);
        }
        
    }

    public removeEvent()
    {
        this.btn_sure.removeTapHandler();
        this.btn_show.removeTapHandler();
        this.btn_close.removeTapHandler();
        this.lb_use.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.useHandle, this);
    }

    public clear()
    {
        super.clear();
        this.awardList = null;
        this.curAward = null;
        this._init = null;
    }
}